package com.example.yunifangdemo.adapter;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.example.yunifangdemo.MainActivity;

import java.util.List;

/**
 * Created by yiqiang on 2018/4/17.
 */

public class GuidePageAdapter extends PagerAdapter{

    private Context mContext;
    private List<ImageView> imageViews;

    public GuidePageAdapter(Context mContext, List<ImageView> imageViews) {
        this.mContext = mContext;
        this.imageViews = imageViews;
    }

    @Override
    public int getCount() {
        return imageViews.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(imageViews.get(position));
        Log.i("股份大股东鼓捣鼓捣",position+"");
        if (position == imageViews.size()-1){
            ImageView imageView = imageViews.get(position);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, MainActivity.class);
                    mContext.startActivity(intent);

                    //关闭如下
                    if(mContext instanceof Activity) {//同样效果
                        if (Activity.class.isInstance(mContext)) {
                            // 转化为activity，然后finish就行了
                            Activity activity = (Activity) mContext;
                            activity.finish();
                        }
                    }
                }
            });

        }
        return imageViews.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(imageViews.get(position));
    }
}
